package com.jr.controller;

import com.jr.entry.PageDto;
import com.jr.pojo.util;
import com.jr.entry.BookInfo;
import com.jr.entry.Users;
import com.jr.service.BookInfoService;
import com.jr.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UsersController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private BookInfoService bookInfoService;
    @RequestMapping("index")
    public String index(){
        return "index";
    }


    @ResponseBody
    @RequestMapping("getall")
    public List<BookInfo> getall(){

        List<BookInfo> bookInfos = bookInfoService.selectAllbook();
        for (BookInfo bookInfo : bookInfos) {
            System.out.println(bookInfo);
        }
        return bookInfos;
    }


    @RequestMapping("login")
    public ModelAndView login(String user_code, String password , HttpSession session, RedirectAttributes redirectAttrs ){
        ModelAndView mv=new ModelAndView();
        Users users=new Users();
        users.setUser_code(user_code);
        users.setPassword(password);
        Users loginUser=usersService.login(users);
        session.setAttribute("user",loginUser);
        //mv.addObject("user",loginUser);
        List<BookInfo> books = bookInfoService.selectAllbook(); // 注意这里应该是返回一个列表
        for (BookInfo book : books) {
            System.out.println(book);
        }
        session.setAttribute("books", books);

        mv.setViewName("show");
        return mv;

    }





    @ResponseBody
    @RequestMapping("selectbook")
        public List<BookInfo> selectbook(Integer book_type,String book_name,Integer is_borrow){
        BookInfo bookInfo=new BookInfo();
        bookInfo.setBook_type(book_type);
        bookInfo.setBook_name(book_name);
        bookInfo.setIs_borrow(is_borrow);
        List<BookInfo> books = bookInfoService.selectbook(bookInfo);
        return books;
    }

   @ResponseBody
   @RequestMapping("deletebook")
   public boolean deletebook( Integer id ){

       boolean deletebook = bookInfoService.deletebook(id);
       return deletebook;
   }

   @ResponseBody
   @RequestMapping("addbooks")
    public  boolean addbooks(util util){
       boolean addbooks = bookInfoService.addbooks(util);
       System.out.println(util);
       return addbooks;
   }

   @ResponseBody
    @RequestMapping("getbookpage")
   public PageDto<BookInfo> getbookpage(@RequestParam(defaultValue = "0") int page,
                                     @RequestParam(defaultValue = "10") int size) {
       int i=7;
       PageDto<BookInfo> getbooks = bookInfoService.getbooks(page, size);
       List<BookInfo> content = getbooks.getContent();
       for (BookInfo bookInfo : content) {
           System.out.println(bookInfo);
       }
       return getbooks;
   }


}
